﻿///////////////////////////////////////////////////////////////////////////////
//
//  This file is part of MathLib.NET.
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//  
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//  
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library;  If not, see 
//  <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////

using System;
using MathLib;
using ILNumerics;
using NUnit.Framework;

namespace MathLibTest
{
    [TestFixture]
    public class ComplexTest
    {
        static double[] mUnwrapTestData = {
            0,
            -1.5728,
            -1.5747,
            -1.5772,
            -1.5790,
            -1.5816,
            -1.5852,
            -1.5877,
            -1.5922,
            -1.5976,
            -1.6044,
            -1.6129,
            -1.6269,
            -1.6512,
            -1.6998,
            -1.8621,
            1.7252,
            1.6124,
            1.5930,
            1.5916,
            1.5708,
            1.5708,
            1.5708 };

        /// <summary>
        /// This test generates a real vector of angles that contain a jump greater than
        /// the default threshold of PI and calls unwrap.
        /// 
        /// The unwrapped vector is tested to ensure that all adjacent angles are
        /// within the default tolerance.
        /// </summary>
        [Test]
        public void unwrap_Ex1()
        {
            // Create vector from unwrap test data
            ILArray<double> angles = new ILArray<double>(mUnwrapTestData);

            // Call unwrap function
            ILArray<double> unwrapped = MLMath.unwrap(angles);

            // Check that no adjacent angles exceed the default tolerance (PI)
            for (int i = 1; i < unwrapped.InternalArray4Experts.Length; i++)
                Assert.IsFalse(Math.Abs(unwrapped.InternalArray4Experts[i] - unwrapped.InternalArray4Experts[i - 1]) > Math.PI);
        }

        /// <summary>
        /// This test generates a complex vector of angles that contain a jump greater than
        /// the default threshold of PI and calls unwrap.
        /// 
        /// The unwrapped vector is tested to ensure that all adjacent angles are
        /// within the default tolerance.
        /// </summary>
        [Test]
        public void unwrap_Ex2()
        {
            // Create vector from unwrap test data
            complex[] complexData = new complex[mUnwrapTestData.Length];
            for (int i = 0; i < mUnwrapTestData.Length; i++)
                complexData[i].real = mUnwrapTestData[i];
            ILArray<complex> angles = new ILArray<complex>(complexData);

            // Call unwrap function
            ILArray<complex> unwrapped = MLMath.unwrap(angles);

            // Check that no adjacent angles exceed the default tolerance (PI)
            for (int i = 1; i < unwrapped.InternalArray4Experts.Length; i++)
                Assert.IsFalse(complex.Abs(unwrapped.InternalArray4Experts[i] - unwrapped.InternalArray4Experts[i - 1]) > Math.PI);
        }
    }
}
